package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.tabs.TabLayout;

import cn.lrwjz.androidguide.R;

/**
 * 标签页示例Activity
 * 展示Material Design标签页组件的使用方法
 */
public class TabsActivity extends AppCompatActivity {
    private TextView contentText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabs);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Tabs 示例");
        }

        contentText = findViewById(R.id.contentText);

        // 设置基础标签页
        TabLayout basicTabs = findViewById(R.id.basicTabs);
        basicTabs.addTab(basicTabs.newTab().setText("标签一"));
        basicTabs.addTab(basicTabs.newTab().setText("标签二"));
        basicTabs.addTab(basicTabs.newTab().setText("标签三"));

        basicTabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                contentText.setText("选中了基础标签页: " + tab.getText());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        // 设置图标标签页
        TabLayout iconTabs = findViewById(R.id.iconTabs);
        iconTabs.addTab(iconTabs.newTab()
                .setIcon(android.R.drawable.ic_dialog_email)
                .setText("邮件"));
        iconTabs.addTab(iconTabs.newTab()
                .setIcon(android.R.drawable.ic_dialog_map)
                .setText("地图"));
        iconTabs.addTab(iconTabs.newTab()
                .setIcon(android.R.drawable.ic_dialog_info)
                .setText("信息"));

        iconTabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                contentText.setText("选中了图标标签页: " + tab.getText());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        // 设置滚动标签页
        TabLayout scrollingTabs = findViewById(R.id.scrollingTabs);
        scrollingTabs.addTab(scrollingTabs.newTab().setText("标签一"));
        scrollingTabs.addTab(scrollingTabs.newTab().setText("标签二"));
        scrollingTabs.addTab(scrollingTabs.newTab().setText("标签三"));
        scrollingTabs.addTab(scrollingTabs.newTab().setText("标签四"));
        scrollingTabs.addTab(scrollingTabs.newTab().setText("标签五"));
        scrollingTabs.addTab(scrollingTabs.newTab().setText("标签六"));
        scrollingTabs.addTab(scrollingTabs.newTab().setText("标签七"));

        scrollingTabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                contentText.setText("选中了滚动标签页: " + tab.getText());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        // 默认选中第一个标签
        contentText.setText("选中了基础标签页: 标签一");
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 